/////////////////////////////////////////////
///该程序实现时间，日期显示
/////////////////////////////////////////////
module smg3(rst,clk_out2,d[23:0],date[23:0],dig[2:0],seg[7:0],key_s);
input clk_out2,rst,key_s;
input [23:0] d,date;
output [2:0] dig;
output [7:0] seg;
reg [7:0] seg;
reg [7:0]seg1,seg2,seg3,seg4,seg5,seg6;

reg [2:0] dig;
parameter _0=8'h3f,_1=8'h06,_2=8'h5b,_3=8'h4f,_4=8'h66,_5=8'h6d,_6=8'h7d,_7=8'h07,_8=8'h7f,_9=8'h6f;

//位选
always@(posedge clk_out2)
begin
if(!rst)
	dig<=3'd0;
	else
	if(dig==3'd7)
	dig<=3'd0;
	else 
	dig<=dig+1;
end
//段选
always @(dig)
begin
	case(dig)
		7:seg<=seg1; //秒个位
		6:seg<=seg2; //秒十位
		5:seg<=8'h40;
		4:seg<=seg3;//分个位
		3:seg<=seg4;//分十位
		2:seg<=8'h40;
		1:seg<=seg5;//时个位
		0:seg<=seg6;//时十位
	endcase
end

always@(d[3:0] or date[3:0])//秒个位或者day个位
begin
if(key_s)
begin
	case(d[3:0])
		0:seg1<=_0;  
		1:seg1<=_1;
		2:seg1<=_2;
		3:seg1<=_3;
		4:seg1<=_4;
		5:seg1<=_5;
		6:seg1<=_6;
		7:seg1<=_7;
		8:seg1<=_8;
		9:seg1<=_9;
	endcase
end
else
begin
	case(date[3:0])
		0:seg1<=_0;  
		1:seg1<=_1;
		2:seg1<=_2;
		3:seg1<=_3;
		4:seg1<=_4;
		5:seg1<=_5;
		6:seg1<=_6;
		7:seg1<=_7;
		8:seg1<=_8;
		9:seg1<=_9;
	endcase
end
end

always@(d[7:4] or date[7:4])//秒十位
begin
if(key_s)
begin
case(d[7:4])
		0:seg2<=_0; 
		1:seg2<=_1; 
		2:seg2<=_2; 
		3:seg2<=_3; 
		4:seg2<=_4; 
		5:seg2<=_5; 
	endcase
end
else
begin
case(date[7:4]) //day d
		0:seg2<=_0; 
		1:seg2<=_1; 
		2:seg2<=_2; 
		3:seg2<=_3;  
	endcase
end
end

always@(d[11:8] or date[11:8])//分个位
begin
if(key_s)
begin
case(d[11:8])
		0:seg3<=_0;  
		1:seg3<=_1;
		2:seg3<=_2;
		3:seg3<=_3;
		4:seg3<=_4;
		5:seg3<=_5;
		6:seg3<=_6;
		7:seg3<=_7;
		8:seg3<=_8;
		9:seg3<=_9;
	endcase
end
else
begin
	case(date[11:8])  //month
		0:seg3<=_0;  
		1:seg3<=_1;
		2:seg3<=_2;
		3:seg3<=_3;
		4:seg3<=_4;
		5:seg3<=_5;
		6:seg3<=_6;
		7:seg3<=_7;
		8:seg3<=_8;
		9:seg3<=_9;
	endcase
end
end


always@(d[15:12] or date[15:12])//分十位
begin
if(key_s)
begin
case(d[15:12])
		0:seg4<=_0; 
		1:seg4<=_1; 
		2:seg4<=_2; 
		3:seg4<=_3; 
		4:seg4<=_4; 
		5:seg4<=_5; 
	endcase
end
else
begin
	case(date[15:12]) //month d
		0:seg4<=_0; 
		1:seg4<=_1; 
		2:seg4<=_2;
	endcase
end
end

always@(d[19:16] or date[19:16])//时个位
begin
if(key_s)
begin
case(d[19:16])
		0:seg5<=_0;  
		1:seg5<=_1;
		2:seg5<=_2;
		3:seg5<=_3;
		4:seg5<=_4;
		5:seg5<=_5;
		6:seg5<=_6;
		7:seg5<=_7;
		8:seg5<=_8;
		9:seg5<=_9;
	endcase
end
else
begin
	case(date[19:16]) //year
		0:seg5<=_0;  
		1:seg5<=_1;
		2:seg5<=_2;
		3:seg5<=_3;
		4:seg5<=_4;
		5:seg5<=_5;
		6:seg5<=_6;
		7:seg5<=_7;
		8:seg5<=_8;
		9:seg5<=_9;
	endcase
end
end

always@(d[23:20] or date[23:20])//时十位
begin
if(key_s)
begin
	case(d[23:20])
		0:seg6<=_0; 
		1:seg6<=_1; 
		2:seg6<=_2; 
	endcase
end
else
begin
	case(date[23:20])  //year d
		0:seg6<=_0;  
		1:seg6<=_1;
		2:seg6<=_2;
		3:seg6<=_3;
		4:seg6<=_4;
		5:seg6<=_5;
		6:seg6<=_6;
		7:seg6<=_7;
		8:seg6<=_8;
		9:seg6<=_9;
	endcase
end

end

endmodule
